package com.leetcode.LC;

public class LC02 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode(0);
        int carry = 0;
        ListNode tmp = listNode;
        while (l1 != null || l2 != null) {
            int sum = 0;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            sum += carry;
            tmp.next = new ListNode(sum % 10);
            carry = sum / 10;
            tmp = tmp.next;
        }
        if (carry > 0) {
            tmp.next = new ListNode(carry);
        }
        return listNode.next;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(2);
        ListNode tmp = listNode1;
        tmp.next = new ListNode(4);
        tmp = tmp.next;
        tmp.next = new ListNode(3);
        tmp = tmp.next;

        ListNode listNode2 = new ListNode(5);
        ListNode tmp2 = listNode2;
        tmp2.next = new ListNode(6);
        tmp2 = tmp2.next;
        tmp2.next = new ListNode(4);
        tmp2 = tmp2.next;
        System.out.println(listNode1.next.val);
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}